<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system 
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : check_permission.class.php
 * ....权限检查
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Mon Jun 01 08:58:42 CST 2009
 */

class CheckPermission extends FrameWork {
	/**
	 * 检查用户组权限
	 * @author workflow Develop Team     
	 * @param  $type检测类型 $member_id会员ID
	 * @param  $param_array 附带个别用户组需要验证的内容
	 * @param  $ajax ajax效果
	 * @return string
	 */
	function memberGroupPermission($type,$member_id,$param_array=array(),$ajax=0){
		//初始化变量
		$this->_getConfiginfo();
		//加载语言包
		$this->getlang("member_group");
		
		require_once('member.class.php');
		$obj_member = new MemberClass();
		$condition['id'] = $member_id;
		$member_row = $obj_member->getMemberInfo($condition,'*','more');
		unset($obj_member);
		if ($member_row['mg_id'] != ''){
			require_once('member_group.class.php');
			$obj_member_group = new MemberGroupClass();
			$group_row = $obj_member_group->getMemberGroupRow($member_row['mg_id']);
			unset($obj_member_group);
		}else {
			self::return_url('',$this->_lang['errMgNotInAnyGroup'],$ajax);
		}
		switch ($type){
			case 'login':
				if ($group_row['mg_iflogin'] == '0'){
					self::return_url('',$this->_lang['errMgDenyLogin'],$ajax);
				}
				break;
			case 'buy':
				if ($group_row['mg_ifbuy'] == '0'){
					self::return_url('',$this->_lang['errMgDenyBuy'],$ajax);
				}
				break;
			case 'sell':
				if ($group_row['mg_ifsell'] == '0'){
					self::return_url('',$this->_lang['errMgDenySell'],$ajax);
				}
				break;
			case 'shop':
				if ($group_row['mg_ifopen_shop'] == '0'){
					self::return_url('',$this->_lang['errMgDenyOpenShop'],$ajax);
				}
				break;
			case 'use_credit':
				if ($group_row['mg_ifuse_credit'] == '0'){
					self::return_url('',$this->_lang['errMgDenyUseCredit'],$ajax);
				}
				break;
			case 'sell_num':
				if ($group_row['mg_sell_num'] == '0'){
					return true;
				}else{
					if($param_array['sell_num'] != ''){//批量上传商品数量时进行验证
						if($param_array['sell_num'] < $group_row['mg_sell_num']){
							return true;
						}else{
							self::return_url('',$this->_lang['errMgDenySellNum'].$group_row['mg_sell_num'],$ajax);
						}
					}else{//添加商品时进行验证
						if($member_row['sell_product_count'] < $group_row['mg_sell_num']){
							return true;
						}else{
							self::return_url('',$this->_lang['errMgDenySellNum'].$group_row['mg_sell_num'],$ajax);
						}
					}
				}
				break;
			default:
				return false;
		}
	}
	
	/**
	 * 抛出返回信息
	 * @author workflow Develop Team     
	 * @param  $return_url 要求返回的地址，可以为空
	 * @param  $return_msg 抛出的内容文字
	 * @param  $ajax 是否是ajax形式 默认为0 不使用ajax
	 * @return bool
	 */
	function return_url($return_url,$return_msg,$ajax=0){
		if($ajax == '1'){
			Common::outMessage('json',$return_msg,0);
		}else{
			$this->redirectPath('error',$return_url,$return_msg);
		}
	}
	
	/**
	 * 会员发布商品权限检查
	 * @author workflow Develop Team     
	 * @param  $member_id 会员ID 
	 * @return bool
	 */
	function outputProductPermission($member_id){
		//初始化变量
		$this->_getConfiginfo();
		//加载语言包
		$this->getlang("member_group");
		
		if ($member_id == ''){
			$this->redirectPath('error','',$this->_lang['errOutPutProductMemberIsEmpty']);
		}

		require_once('member.class.php');
		$obj_member = new MemberClass();
		$condition['id'] = $member_id;
		$member_row = $obj_member->getMemberInfo($condition,'*','more');
		unset($obj_member);
		if (!$member_row){
			$this->redirectPath('error','',$this->_lang['errOutPutProductMemberIsEmpty']);
		}
		
		//店铺信息
		require_once('shop.class.php');
		$obj_shop = new ShopClass();
		$shop_row = $obj_shop->getOneShopByMemeberId($member_id);
		unset($obj_shop);
		if ($this->_configinfo['productinfo']['output_condition'] != ''){
			$array = array();
			$array = explode(',',$this->_configinfo['productinfo']['output_condition']);
			foreach ($array as $k => $v){
				switch ($v){
					case 'shop'://开店
						if ($shop_row === false){
							$this->redirectPath('error','../member/own_shop.php?action=new',$this->_lang['errOutPutProductNoShop']);
						}
						break;
					case 'certify'://个人实名
						if ($member_row['personal_certify'] != '2'){
							$this->redirectPath('error','../member/own_member.php?action=personal_certify',$this->_lang['errOutPutProductNoCertify']);
						}
						break;
					case 'entity'://实体店铺
						if ($shop_row['audit_state'] != '2'){
							$this->redirectPath('error','../member/own_shop.php?action=entity_check',$this->_lang['errOutPutProductNoEntity']);
						}
						break;
				}
			}
		}
		return true;
	}
}

?>